package com.hd.crdao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.BankDataBean;
import com.hd.crdao.bean.CbRecaccountBean;
import com.hd.crdao.bean.MsgBean;
import com.hd.crdao.bean.RecAccountStatusBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/8/17 0017
 */

public class GetMoneyActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.getmoney_head)
    ExpandHeadView mGetmoneyHead;
    @BindView(R.id.status)
    TextView mStatus;
    @BindView(R.id.T1text)
    TextView mT1Text;   //费率t1
    @BindView(R.id.T0text)
    TextView mT0Text;   //费率t0
    @BindView(R.id.tv_bank)
    TextView mTvBank;   //结算银行
    @BindView(R.id.account_name)
    TextView mAccountName;   //结算账户名称
    @BindView(R.id.tv_account)
    TextView mTvAccount;    //结算账户
    @BindView(R.id.contacts)
    TextView mContacts;   //联系人
    @BindView(R.id.phone)
    TextView mPhone;   //联系电话
    @BindView(R.id.bt_sure)
    Button mBtSure;
    @BindView(R.id.ll_unopen)
    LinearLayout mLlUnopen;
    @BindView(R.id.ll_opened)
    LinearLayout mLlOpened;
    public String BankCardInfoId;  //银行卡账号
    private int bankCardStatus;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_getmoney;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initData() {
        mBtSure.setOnClickListener(this);
        mTvBank.setOnClickListener(this);
        loadData(SPUtils.getString(Config.businessId, ""));
        dealWithTextView(mT0Text);
        dealWithTextView(mT1Text);
    }

    private void dealWithTextView(TextView targetView) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(targetView.getText().toString());
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(Color.parseColor("#2c2c2c"));
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(Color.parseColor("#666666"));
        spannableStringBuilder.setSpan(colorSpan1,0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.setSpan(colorSpan2,4,9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        targetView.setText(spannableStringBuilder);

    }

    @Override
    protected void initListener() {
        mGetmoneyHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    /**
     * 车商收款账户申请
     */
    private void applyRecAccount() {
        if (TextUtils.isEmpty(mTvAccount.getText().toString())) {
            MyToast.show("请先选择银行卡");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("carBusinessId", SPUtils.getString(Config.businessId, ""));
        map.put("businessCardId", BankCardInfoId);
        map.put("contactsName", mContacts.getText().toString().trim());
        map.put("contactsPhone", mPhone.getText().toString().trim());
        map.put("sign", Constants.SIGN);
        OkHttpUtils.post().url(Constants.APPLYRECACCOUNT_URL)
                .params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                MsgBean msgBean = new Gson().fromJson(response, MsgBean.class);
                if (msgBean.getSuccess() == 0) {
                    MyToast.show(msgBean.getMessage());
                    startActivity(new Intent(UIUtils.getContext(), GetMoneyActivity.class));
                    finish();
                } else {
                    MyToast.show(msgBean.getMessage());
                }
            }
        });
    }

    /**
     * 初始化数据
     *
     * @param busnissId
     */
    private void loadData(String busnissId) {
        OkHttpUtils.post().url(Constants.GET_RECACCOUNT_STATUS_URL).addParams("carBusinessId", busnissId).addParams("sign", Constants.SIGN)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                try {
                    JSONObject json = new JSONObject(response);
                    String success = (String) json.get("success");
                    String message = (String) json.get("message");

                    if (TextUtils.equals("00", success)) {
                        mLlOpened.setVisibility(View.VISIBLE);
                        mLlUnopen.setVisibility(View.GONE);
                        Gson gson = new Gson();
                        RecAccountStatusBean recAccountStatusBean = gson.fromJson(response, RecAccountStatusBean.class);
                        String bcName = recAccountStatusBean.getCbRecaccount().getBusinessCard().getBcName();  //结算账户名称
                        String bcNote = recAccountStatusBean.getCbRecaccount().getBusinessCard().getBankCode().getBcNote();  //结算银行
                        String bcCardnum = recAccountStatusBean.getCbRecaccount().getBusinessCard().getBcCardnum();  //结算账户
                      //  double crRate = recAccountStatusBean.getCbRecaccount().getCrRate();   // 结算费率
                        String contactsName = recAccountStatusBean.getCbRecaccount().getContactsName();  // 联系人
                        String contactsPhone = recAccountStatusBean.getCbRecaccount().getContactsPhone();//联系电话
                       // mTvRate.setText(crRate + "%");
                        mTvBank.setText(bcNote);
                        mAccountName.setText(bcName);
                        mTvAccount.setText(bcCardnum);
                        mContacts.setText(contactsName);
                        mPhone.setText(contactsPhone);
                        mBtSure.setText("我要收款");
                        mTvBank.setOnClickListener(null);
                    } else if (TextUtils.equals("01", success)) {
                        MyToast.show(message);
                    } else if (TextUtils.equals("02", success)) {       //就是没有绑定过银行卡
                        bankCardStatus = Integer.parseInt(success);
                        mLlOpened.setVisibility(View.GONE);
                        mLlUnopen.setVisibility(View.VISIBLE);
                        Gson gson = new Gson();
                        RecAccountStatusBean recAccountStatusBean = gson.fromJson(response, RecAccountStatusBean.class);
                      //  double crRate = recAccountStatusBean.getCbRecaccount().getCrRate();   // 结算费率
                        String contactsName = recAccountStatusBean.getCbRecaccount().getContactsName();  // 联系人
                        String contactsPhone = recAccountStatusBean.getCbRecaccount().getContactsPhone();//联系电话
                        //mTvRate.setText(crRate + "%");
                        mContacts.setText(contactsName);
                        mPhone.setText(contactsPhone);
                        mBtSure.setText("确定");
                    } else if (TextUtils.equals("03", success)) {       //这个出现情况在银行列表已经申请了银行卡,可以直接拿到信息
                        String cbRecaccount = json.getString("cbRecaccount");
                        CbRecaccountBean cbRecaccountBean = new Gson().fromJson(cbRecaccount, CbRecaccountBean.class);
                        CbRecaccountBean.BusinessCardBean businessCard = cbRecaccountBean.getBusinessCard();
                        //mTvRate.setText(cbRecaccountBean.getCrRate() + "%");
                        mContacts.setText(cbRecaccountBean.getContactsName());
                        mPhone.setText(cbRecaccountBean.getContactsPhone());
                        mTvBank.setText(businessCard.getBankCode().getBcNote());
                        mAccountName.setText(businessCard.getBcName());
                        mTvAccount.setText(businessCard.getBcCardnum());
                        BankCardInfoId = businessCard.getId();
                    } else if (TextUtils.equals("04", success)) {
                        MyToast.show(message);
                    } else if (TextUtils.equals("05", success)) {
                        mTvBank.setOnClickListener(null);
                        mLlOpened.setVisibility(View.GONE);
                        mLlUnopen.setVisibility(View.VISIBLE);
                        mStatus.setVisibility(View.VISIBLE);
                        Gson gson = new Gson();
                        RecAccountStatusBean recAccountStatusBean = gson.fromJson(response, RecAccountStatusBean.class);
                        String bcName = recAccountStatusBean.getCbRecaccount().getBusinessCard().getBcName();  //结算账户名称
                        String bcNote = recAccountStatusBean.getCbRecaccount().getBusinessCard().getBankCode().getBcNote();  //结算银行
                        String bcCardnum = recAccountStatusBean.getCbRecaccount().getBusinessCard().getBcCardnum();  //结算账户
                        double crRate = recAccountStatusBean.getCbRecaccount().getCrRate();   // 结算费率
                        String contactsName = recAccountStatusBean.getCbRecaccount().getContactsName();  // 联系人
                        String contactsPhone = recAccountStatusBean.getCbRecaccount().getContactsPhone();//联系电话
                        //mTvRate.setText(crRate + "%");
                        mTvBank.setText(bcNote);
                        mAccountName.setText(bcName);
                        mTvAccount.setText(bcCardnum);
                        mContacts.setText(contactsName);
                        mPhone.setText(contactsPhone);
                        mBtSure.setVisibility(View.GONE);
                    } else if (TextUtils.equals("07", success)) {
                        MyToast.show(message);
                        mTvBank.setOnClickListener(null);
                    } else {
                        MyToast.show(message);
                    }
                } catch (JSONException e) {
                    ALog.d(e.getMessage());
                }
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //选择结算银行传过来的值
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getBankData(BankDataBean bankDataBean) {
        mTvBank.setText(bankDataBean.getBankName());
        mAccountName.setText(bankDataBean.getAccountName());
        mTvAccount.setText(bankDataBean.getBankNumber());
        BankCardInfoId = bankDataBean.getId();
        ALog.d(BankCardInfoId);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_sure:
                ALog.d("确认键被点击");
                String tipContent = mBtSure.getText().toString();
                if (TextUtils.equals("确定", tipContent)) {
                    ALog.d("申请方法走了没?");
                    applyRecAccount();
                } else if (TextUtils.equals("我要收款", tipContent)) {
                    startActivity(new Intent(UIUtils.getContext(), SetAmountActivity.class));
                }
                break;
            case R.id.tv_bank:      //选择银行卡
                Intent intent;
                if (bankCardStatus == 2) {
                    intent = new Intent(UIUtils.getContext(), AddBankCardActivity.class);
                } else {
                    intent = new Intent(UIUtils.getContext(), MyBankCardActivity.class);
                }
                intent.putExtra("isWannaData", true);
                startActivity(intent);
                break;
        }
    }

}
